package com.hsp.learn.sparsearray;

import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * @Author Zan
 * @Create 2024/9/11 15:03
 * @ClassName: SparseArray
 * @Description : 稀疏数组
 */
public class SparseArray {
    public static void main(String[] args) {
        // 创建一个原始的二位数据 11 * 11
        // 0：表示没有棋子
        // 1：表示黑子
        // 2：表示蓝子
        int[][] chessArr1 = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[4][7] = 2;
        // 输出原始的二维数组
        System.out.println("原始的二维数组~~~");
        for (int[] row : chessArr1) {
            for (int col : row) {
                System.out.printf("%d\t", col);
            }
            System.out.println("\n");
        }

        // 将二位数组转成稀疏数组
        // 1.先遍历二位数据，得到非0数据的个数
        int sum = 0;
        for (int[] row : chessArr1) {
            for (int col : row) {
                if (col != 0) {
                    sum++;
                }
            }
        }
//        System.out.println(sum);

        // 2.创建对应的稀疏数组sparseArr
        int[][] sparseArr = new int[sum + 1][3];
        // 给稀疏数组赋值
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;

        // 遍历二维数组，将非0的值存放到稀疏数组sparseArr中
        // count 用于记录是第几个非0数据
        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }

        // 输出稀疏数组的形式
        System.out.println("得到的稀疏数组~~~");
        for (int[] row : sparseArr) {
            for (int col : row) {
                System.out.printf("%d\t", col);
            }
            System.out.println("\n");
        }

        // 将稀疏数组恢复成原始的二维数组
        // 1.先读取稀疏数组的第一行，根据第一行的数据，创建原始的二维数组
        int[][] chessArr2 = new int[sparseArr[0][0]][sparseArr[0][1]];

        // 2.再读取稀疏数组后几行的数据，并赋给原始的二维数组即可
        for (int i = 1; i < sparseArr.length; i++) {
            chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }

        // 输出恢复后的二维数组
        System.out.println("恢复后的二维数组~~~");
        for (int[] row : chessArr2) {
            for (int col : row) {
                System.out.printf("%d\t", col);
            }
            System.out.println("\n");
        }
    }

    /**
     * 将稀疏数组保存到磁盘文件
     */
    public static void saveSparseArrayToFile(int[][] sparseArr, String filePath) throws IOException {
        FileWriter fileWriter = new FileWriter(filePath);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

        for (int[] row : sparseArr) {
            bufferedWriter.write(row[0] + " " + row[1] + " " + row[2]);
            bufferedWriter.newLine();
        }

        bufferedWriter.close();
    }

    /**
     * 从磁盘文件读取稀疏数组
     */
    public static int[][] readSparseArrayFromFile(String filePath) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath));
        String line = bufferedReader.readLine();
        String[] firstRow = line.split(" ");
        int rows = Integer.parseInt(firstRow[2]);

        int[][] sparseArr = new int[rows + 1][3];
        sparseArr[0][0] = Integer.parseInt(firstRow[0]);
        sparseArr[0][1] = Integer.parseInt(firstRow[1]);
        sparseArr[0][2] = Integer.parseInt(firstRow[2]);

        int rowIndex = 1;
        while ((line = bufferedReader.readLine()) != null) {
            String[] data = line.split(" ");
            sparseArr[rowIndex][0] = Integer.parseInt(data[0]);
            sparseArr[rowIndex][1] = Integer.parseInt(data[1]);
            sparseArr[rowIndex][2] = Integer.parseInt(data[2]);
            rowIndex++;
        }

        bufferedReader.close();
        return sparseArr;
    }

    @Test
    public void test02() throws IOException {
        // 创建一个原始的二位数据 11 * 11
        int[][] chessArr1 = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[4][7] = 2;

        // 输出原始的二维数组
        System.out.println("原始的二维数组~~~");
        for (int[] row : chessArr1) {
            for (int col : row) {
                System.out.printf("%d\t", col);
            }
            System.out.println("\n");
        }

        // 将二位数组转成稀疏数组
        int sum = 0;
        for (int[] row : chessArr1) {
            for (int col : row) {
                if (col != 0) {
                    sum++;
                }
            }
        }

        // 创建对应的稀疏数组
        int[][] sparseArr = new int[sum + 1][3];
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;

        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }

        // 输出稀疏数组
        System.out.println("得到的稀疏数组~~~");
        for (int[] row : sparseArr) {
            for (int col : row) {
                System.out.printf("%d\t", col);
            }
            System.out.println("\n");
        }

        // 将稀疏数组保存到磁盘
        saveSparseArrayToFile(sparseArr, "map.data");

        // 从磁盘文件读取稀疏数组
        int[][] sparseArrFromFile = readSparseArrayFromFile("map.data");

        // 将稀疏数组恢复成原始的二维数组
        int[][] chessArr2 = new int[sparseArrFromFile[0][0]][sparseArrFromFile[0][1]];
        for (int i = 1; i < sparseArrFromFile.length; i++) {
            chessArr2[sparseArrFromFile[i][0]][sparseArrFromFile[i][1]] = sparseArrFromFile[i][2];
        }

        // 输出恢复后的二维数组
        System.out.println("恢复后的二维数组~~~");
        for (int[] row : chessArr2) {
            for (int col : row) {
                System.out.printf("%d\t", col);
            }
            System.out.println("\n");
        }
    }
}
